package net.jalbum.filters;

import java.awt.event.ActionEvent;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JSlider;

import net.jalbum.filters.components.GammaSlider;
import se.datadosen.component.RiverLayout;
import se.datadosen.jalbum.Msg;

public class GammaControl extends FilterControl {

    /**
     *
     */
    private static final long serialVersionUID = 2472355637938881568L;
    protected GammaFilterPlugin gammaFilterPlugin;
    protected GammaSlider sliderAll;
    protected GammaSlider sliderRed;
    protected GammaSlider sliderGreen;
    protected GammaSlider sliderBlue;
    protected JLabel labelRed;
    protected JLabel labelGreen;
    protected JLabel labelBlue;
    protected JButton resetAll;

    protected JCheckBox individualSettings;

    protected double gammaValueAll = 1;
    protected double gammaValueRed = 1;
    protected double gammaValueGreen = 1;
    protected double gammaValueBlue = 1;

    public GammaControl() {
    }

    public GammaControl(GammaFilterPlugin gammaFilterPlugin) {
        init(gammaFilterPlugin);

        addGammaControls();
    }

    protected void addGammaControls() {
        sliderAll = new GammaSlider(JSlider.HORIZONTAL);
        sliderAll.getSlider().setPaintLabels(false);
        sliderAll.setSliderWidth(SLIDER_WIDTH);
        sliderAll.removeResetButton();
        sliderAll.addActionListener(this);

        sliderRed = new GammaSlider(JSlider.HORIZONTAL);
        sliderRed.getSlider().setPaintLabels(false);
        sliderRed.setSliderWidth(SLIDER_WIDTH);
        sliderRed.removeResetButton();
        sliderRed.setEnabled(false);
        sliderRed.addActionListener(this);

        sliderGreen = new GammaSlider(JSlider.HORIZONTAL);
        sliderGreen.getSlider().setPaintLabels(false);
        sliderGreen.setSliderWidth(SLIDER_WIDTH);
        sliderGreen.removeResetButton();
        sliderGreen.setEnabled(false);
        sliderGreen.addActionListener(this);

        sliderBlue = new GammaSlider(JSlider.HORIZONTAL);
        sliderBlue.getSlider().setPaintLabels(false);
        sliderBlue.setSliderWidth(SLIDER_WIDTH);
        sliderBlue.removeResetButton();
        sliderBlue.setEnabled(false);
        sliderBlue.addActionListener(this);

        labelRed = new JLabel(Msg.getString(this, "filters.gamma.gammaRed"));
        labelRed.setEnabled(false);
        labelGreen = new JLabel(Msg.getString(this, "filters.gamma.gammaGreen"));
        labelGreen.setEnabled(false);
        labelBlue = new JLabel(Msg.getString(this, "filters.gamma.gammaBlue"));
        labelBlue.setEnabled(false);

        individualSettings = new JCheckBox();
        individualSettings.addActionListener(this);

        resetAll = new JButton(Msg.getString(this, "filters.colorAdjustment.resetAll"));
        resetAll.addActionListener(this);

        this.add(new JLabel(Msg.getString(this, "filters.gamma.gammaAll")));
        this.add("br", sliderAll);
        this.add("p", individualSettings);
        this.add(new JLabel(Msg.getString(this, "filters.gamma.enable")));
        this.add("br", labelRed);
        this.add("br", sliderRed);
        this.add("p", labelGreen);
        this.add("br", sliderGreen);
        this.add("p", labelBlue);
        this.add("br", sliderBlue);
        this.add("p", resetAll);
    }

    protected void init(GammaFilterPlugin gammaFilterPlugin) {
        this.gammaFilterPlugin = gammaFilterPlugin;

        super.init(gammaFilterPlugin);
        setLayout(new RiverLayout(6, 2));

    }

    public void actionPerformed(ActionEvent e) {
        super.actionPerformed(e);

        if (e.getSource() == sliderAll) {
            gammaValueAll = sliderAll.getResultingGamma();
            gammaFilterPlugin.setGammaAlpha(gammaValueAll);
            if (!individualSettings.isSelected()) {
                gammaValueRed = gammaValueAll;
                gammaFilterPlugin.setGammaR(gammaValueRed);
                gammaValueGreen = gammaValueAll;
                gammaFilterPlugin.setGammaG(gammaValueGreen);
                gammaValueBlue = gammaValueAll;
                gammaFilterPlugin.setGammaB(gammaValueBlue);
            }
            gammaFilterPlugin.renderImage();

        } else if (e.getSource() == sliderRed) {
            this.gammaValueRed = sliderRed.getResultingGamma();
            gammaFilterPlugin.setGammaR(gammaValueRed);
            gammaFilterPlugin.renderImage();

        } else if (e.getSource() == sliderGreen) {
            this.gammaValueGreen = sliderGreen.getResultingGamma();
            gammaFilterPlugin.setGammaG(gammaValueGreen);
            gammaFilterPlugin.renderImage();

        } else if (e.getSource() == sliderBlue) {
            this.gammaValueBlue = sliderBlue.getResultingGamma();
            gammaFilterPlugin.setGammaB(gammaValueBlue);
            gammaFilterPlugin.renderImage();

        } else if (e.getSource() == individualSettings) {
            sliderRed.setEnabled(individualSettings.isSelected());
            sliderGreen.setEnabled(individualSettings.isSelected());
            sliderBlue.setEnabled(individualSettings.isSelected());
            labelRed.setEnabled(individualSettings.isSelected());
            labelGreen.setEnabled(individualSettings.isSelected());
            labelBlue.setEnabled(individualSettings.isSelected());

            if (!individualSettings.isSelected()) {
                gammaValueRed = gammaValueAll;
                gammaValueGreen = gammaValueAll;
                gammaValueBlue = gammaValueAll;
            } else {
                gammaValueRed = sliderRed.getResultingGamma();
                gammaValueGreen = sliderGreen.getResultingGamma();
                gammaValueBlue = sliderBlue.getResultingGamma();
            }
            gammaFilterPlugin.setGammaR(gammaValueRed);
            gammaFilterPlugin.setGammaG(gammaValueGreen);
            gammaFilterPlugin.setGammaB(gammaValueBlue);
            gammaFilterPlugin.renderImage();

        } else if (e.getSource() == resetAll) {
            sliderAll.reset();
            sliderRed.reset();
            sliderGreen.reset();
            sliderBlue.reset();
            sliderRed.setEnabled(false);
            sliderGreen.setEnabled(false);
            sliderBlue.setEnabled(false);
            labelRed.setEnabled(false);
            labelGreen.setEnabled(false);
            labelBlue.setEnabled(false);
            individualSettings.setSelected(false);
        }
    }
}
